package com.ruoyi.api.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.DzKaoshi;
import com.ruoyi.system.domain.DzShijuan;
import com.ruoyi.system.service.IDzKaoshiService;
import com.ruoyi.system.service.IDzShijuanService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DzKsjieguo;
import com.ruoyi.system.service.IDzKsjieguoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试结果Controller
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/api/ksjieguo")
public class DzKsjieguoApiController extends BaseController
{
    @Autowired
    private IDzKsjieguoService dzKsjieguoService;

    @Autowired
    private IDzKaoshiService dzKaoshiService;

    @Autowired
    private IDzShijuanService dzShijuanService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询考试结果列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DzKsjieguo dzKsjieguo)
    {
        startPage();
        List<DzKsjieguo> list = dzKsjieguoService.selectDzKsjieguoListWeb(dzKsjieguo);
        return getDataTable(list);
    }

    /**
     * 导出考试结果列表
     */
    @Log(title = "考试结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DzKsjieguo dzKsjieguo)
    {
        List<DzKsjieguo> list = dzKsjieguoService.selectDzKsjieguoListWeb(dzKsjieguo);
        ExcelUtil<DzKsjieguo> util = new ExcelUtil<DzKsjieguo>(DzKsjieguo.class);
        util.exportExcel(response, list, "考试结果数据");
    }

    /**
     * 获取考试结果详细信息
     */
    @GetMapping(value = "/{jgId}")
    public AjaxResult getInfo(@PathVariable("jgId") Long jgId)
    {
        return AjaxResult.success(dzKsjieguoService.selectDzKsjieguoByJgId(jgId));
    }

    /**
     * 新增考试结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody DzKsjieguo dzKsjieguo)
    {
        List<DzKsjieguo> list = dzKsjieguoService.selectDzKsjieguoListWeb(dzKsjieguo);

        for (DzKsjieguo obj :list){
            dzKsjieguoService.deleteDzKsjieguoByJgId(obj.getJgId());
        }

        SysDept dept = sysDeptService.selectDeptById(dzKsjieguo.getKsdeptId());
        if(dept!=null){
            dzKsjieguo.setDeptName(dept.getDeptName());
        }

        dzKsjieguoService.insertDzKsjieguo(dzKsjieguo);

        return AjaxResult.success(dzKsjieguo);
    }

    /**
     * 修改考试结果
     */
    @Log(title = "考试结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DzKsjieguo dzKsjieguo)
    {
//        if(dzKsjieguo.getKsdeptId()!=null){
//            //
//            SysDept dept =  sysDeptService.selectDeptById(dzKsjieguo.getKsdeptId());
//            if(dept!=null&&dept.getAncestors()!=null){
//                String[] arr = dept.getAncestors().split(",");
//                if(arr!=null&&arr.length>2){
//                           String newdeptid = arr[2];
//                    SysDept dept2 =  sysDeptService.selectDeptById(Long.parseLong(newdeptid));
//                    dzKsjieguo.setDeptName(dept2.getDeptName());
//                }
//            }
//
//        }
        if(dzKsjieguo.getKsType()==0){
           DzShijuan shijuan =  dzShijuanService.selectDzShijuanBySjId(dzKsjieguo.getKsId());
           if(shijuan!=null){
               SysDept dept2 =  sysDeptService.selectDeptById(shijuan.getDeptId());
               if(dept2!=null){
                   dzKsjieguo.setDeptName(dept2.getDeptName());
               }
           }
        }

        if(dzKsjieguo.getKsType()==1){
            DzKaoshi kaoshi = dzKaoshiService.selectDzKaoshiByKsId(dzKsjieguo.getKsId());
            if(kaoshi!=null){
                SysDept dept2 =  sysDeptService.selectDeptById(kaoshi.getDeptId());
                if(dept2!=null){
                    dzKsjieguo.setDeptName(dept2.getDeptName());
                }
            }
        }



        return toAjax(dzKsjieguoService.updateDzKsjieguo(dzKsjieguo));
    }

    /**
     * 删除考试结果
     */
    @Log(title = "考试结果", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult remove(@RequestBody Long[] jgIds)
    {
        return toAjax(dzKsjieguoService.deleteDzKsjieguoByJgIds(jgIds));
    }
}
